package com.hsoft.zhxy.app.xtgl_02_01.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsoft.zhxy.app.common.model.entity.XtcsglEntity;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.XtcsglMapper;
import com.hsoft.zhxy.app.xtgl_02_01.service.XtcsglService;
import com.hsoft.zhxy.common.base.BaseService;
import com.hsoft.zhxy.common.result.ResultBody;
import com.hsoft.zhxy.common.util.HttpRequestLocal;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Log4j2
@Service
public class XtcsglServiceImpl extends BaseService implements XtcsglService {

    @Autowired
    XtcsglMapper xtcsglMapper;
    @Autowired
    private HttpRequestLocal httpRequestLocal;

    /**
     * 查询单条数据
     * @param xtcsglEntity
     * @return
     */
    public JSONObject findOne(XtcsglEntity xtcsglEntity) {
        XtcsglEntity rs = xtcsglMapper.selectOne(new QueryWrapper<XtcsglEntity>().setEntity(xtcsglEntity));
        return ResultBody.setData(rs);
    }

    /**
     * 查询列表数据
     * @param xtcsglEntity
     * @return
     */
    public JSONObject findList(XtcsglEntity xtcsglEntity) {
        List<XtcsglEntity> list = xtcsglMapper.selectList(new QueryWrapper<XtcsglEntity>().setEntity(xtcsglEntity));
        return ResultBody.setData(list);
    }

    /**
     * 查询分页数据
     * @param xtcsglEntity
     * @param page
     * @param pageSize
     * @return
     */
    public JSONObject findListByPage(XtcsglEntity xtcsglEntity, long page, long pageSize) {
        Page<XtcsglEntity> pageObj = new Page<XtcsglEntity>(page, pageSize);
        IPage<XtcsglEntity> result = xtcsglMapper.selectPage(pageObj, new QueryWrapper<XtcsglEntity>().setEntity(xtcsglEntity));
        return ResultBody.setData(result.getTotal(),  result.getRecords());
    }

    /**
     * 新增
     *
     * @param xtcsglEntity
     * @return
     */
    public JSONObject add(XtcsglEntity xtcsglEntity) {
        xtcsglEntity.setCjsj(new Date());
        xtcsglEntity.setCjr(getYhid());
        xtcsglEntity.setBgsj(new Date());
        xtcsglEntity.setBgr(getYhid());
        return ResultBody.setData(xtcsglMapper.insert(xtcsglEntity));
    }

    /**
     * 修改
     *
     * @param xtcsglEntity
     * @return
     */

    public JSONObject update(XtcsglEntity xtcsglEntity) {
        xtcsglEntity.setBgsj(new Date());
        xtcsglEntity.setBgr(getYhid());
        return ResultBody.setData(xtcsglMapper.updateById(xtcsglEntity));
    }

    /**
     * 删除
     *
     * @param xtcsglEntity
     * @return
     */
    public JSONObject delete(XtcsglEntity xtcsglEntity) {
        return ResultBody.setData(xtcsglMapper.delete(new QueryWrapper<XtcsglEntity>().setEntity(xtcsglEntity)));
    }

}
